extends Node2D

class_name Block

var bounds = {
	"min_x":-216,
	"max_x":216,
	"max_y":490,
}  # 这个是整个布局的边界再加上半个block的宽度

var wall_kicks

var block_cell

var block_data

var block_unit_arr = []

@onready var block_unit_scene = preload("res://Scenes/block_unit.tscn")
@onready var timer = $Timer
var target = global_position
var current_block_unit
@export var speed = 800
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	print("block ready")
	var block_unit = block_unit_scene.instantiate() as BlockUnit
	print("block global_position"+str(global_position))
	block_unit.position = global_position
	block_unit_arr.append(block_unit)
	add_child(block_unit)
	block_unit.set_texture(block_data.piece_texture)
	print("block_unit positon"+str(block_unit.position))
	#block_unit.position = global_position
	current_block_unit = block_unit
	#block_unit.block_unit_is_dragging.connect(on_block_unit_is_dragging)

func on_block_unit_is_dragging(is_dragging):
	print("block on_block_unit_is_dragging"+str(is_dragging))
	if is_dragging :
		timer.stop()
	else: 
		timer.start()
	pass

		
		
		
		
		
#func _input(event):
	#pass
	#if event.is_action_pressed("click"):
	#if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		#if event.pressed:
			#target = get_global_mouse_position()
			#print(target)
			#_select_character_at_position(target)
			#print('fdsdee')
			#print(current_block_unit)
			#if(current_block_unit is Area2D):
				#current_block_unit.is_dragging=true
				#current_block_unit.drag_offset = current_block_unit.position - target
				#current_block_unit.target = target
				#current_block_unit.velocity = global_position.direction_to(target) * speed
				#current_block_unit.set_physics_process(true)
		#else:
			#_select_character_at_position(get_global_mouse_position())
	#else:
		#current_block_unit.is_dragging = false
		#current_block_unit.set_physics_process(false)
	

func move(direction:Vector2):
	var new_position = calculate_global_position(direction,global_position)
	print(new_position)
	if new_position:
		global_position = new_position
		#if direction != Vector2.DOWN:
			#hard_drop_ghost.call_deferred()
		return true
	return false

func calculate_global_position(direction:Vector2,starting_global_position:Vector2):

	##check for collision with other tetromino
	#
	#if is_colliding_with_other_tetromino(direction,starting_global_position):
		#return null
	#
	##check for collision with game bounds
	if !is_within_game_bounds(direction,starting_global_position):
		print("with game bounds")
		return null
	return starting_global_position+direction*block_unit_arr[0].get_size().x

func is_colliding_with_other_tetromino(direction:Vector2,starting_global_position:Vector2):
	pass
	#for tetromino in other_tetrominos:
		#var tetromino_pieces = tetromino.get_children().filter(func (c):return c is Piece)
		#for tetromino_piece in tetromino_pieces:
			#for piece in pieces:
				#if starting_global_position + piece.position + direction*piece.get_size().x == tetromino.global_position + tetromino_piece.position:
					#return true
	#return false

func is_within_game_bounds(direction:Vector2,starting_global_position:Vector2):
	for piece in block_unit_arr:
		var new_position = piece.position + starting_global_position + direction*piece.get_size()
		print(new_position)
		if new_position.x < bounds.get("min_x") || new_position.x > bounds.get("max_x") || new_position.y > bounds.get("max_y"):
			return false
	return true	
	
func lock():
	timer.stop()
	#lock_tetromino.emit(self)
	#set_process_input(false)
	#ghost_tetromino.queue_free()

func _on_timer_timeout() -> void:
	#pass
	var should_lock = !move(Vector2.DOWN)
	if should_lock:
		lock()
